package es.aa.android.cartomatic.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import es.aa.android.cartomatic.R;

public class CountOptionView extends RelativeLayout {
	
	private Button minus;
	private Button plus;
	private TextView count;

	public CountOptionView(Context context) {
		super(context);
		init(context);
	}
	
	public CountOptionView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	private void init(Context context) {
		LayoutInflater.from(context).inflate(R.layout.count, this, true);
		minus = (Button) findViewById(R.id.buttonMinus);
		plus = (Button) findViewById(R.id.buttonPlus);
		count = (TextView) findViewById(R.id.textViewCount);
		count.setText("0");
		makeActions();
	}

	private void makeActions() {
		minus.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				count.setText(String.valueOf(getCountNum()-1));
			}
		});

		plus.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				count.setText(String.valueOf(getCountNum()+1));
			}
		});
	}
	
	private int getCountNum() {
		String n = count.getText().toString();
		int num = Integer.valueOf(n);
		return num;
	}

}
